#include <stddef.h>           /* needed for definition of NULL */
#include "Common.h"
#include "LCD.h"
#include "Gdi.h"
/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       DB2SJIS
*/
static WORD DB2SJIS(BYTE Byte0, BYTE Byte1) {
    return Byte1 | (((WORD)Byte0)<<8);
}

/*********************************************************************
*
*       _GetLineDistX_SJIS
*/
static int _GetLineDistX_SJIS(const char *s, int Len) {
    int Dist =0;
    if (s) {
        BYTE c0;
        while (((c0=*(const BYTE*)s) !=0) && Len >=0) {
            s++; Len--;
            if (c0 > 127) {
                BYTE  c1 = *(const BYTE*)s++;
                Len--;
                Dist += GUI_GetCharDistX(DB2SJIS(c0, c1));
            } else {
                Dist += GUI_GetCharDistX(c0);
            }
        }
    }
    return Dist;
}

/*********************************************************************
*
*       _GetLineLen_SJIS
* Purpose:
*   Returns the number of characters in a string.
*
* NOTE:
*   The return value can be used as offset into the
*   string, which means that double characters count double
*/
static int _GetLineLen_SJIS(const char *s, int MaxLen) {
    int Len =0;
    BYTE c0;
    while (((c0=*(const BYTE*)s) !=0) && Len < MaxLen) {
        s++;
        if (c0 > 127) {
            Len++; s++;
        } else {
            switch (c0) {
                case '\n': return Len;
            }
        }
        Len++;
    }
    return Len;
}

/*********************************************************************
*
*       _DispLine_SJIS
*/
static void _DispLine_SJIS(const char *s, int Len) {
    BYTE c0;
    while (--Len >=0) {
        c0=*(const BYTE*)s++;
        if (c0 > 127) {
            BYTE  c1 = *(const BYTE*)s++;
            Len--;
            GL_DispChar (DB2SJIS(c0, c1));
        } else {
            GL_DispChar(c0);
        }
    }
}

/*********************************************************************
*
*       GUI_ENC_APIList_SJIS, API list
*/
const tGUI_ENC_APIList GUI_ENC_APIList_SJIS = {
    _GetLineDistX_SJIS,
    _GetLineLen_SJIS,
    _DispLine_SJIS
};

/*************************** End of file ****************************/
